/*
 * Copyright 2011 Kim Lindhardt Madsen
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package dk.lindhardt.gwt.geie.client.edit;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.*;
import com.google.gwt.user.client.ui.*;
import dk.lindhardt.gwt.geie.client.image.GeieImages;
import dk.lindhardt.gwt.geie.shared.CellFormat;

/**
 * User: AnAmuser
 * Date: 18-08-11
 * <p/>
 * Styles fonts
 */
public class TextStyleMenu extends Composite implements StyleMenu {

   private static GeieImages imageResources = GWT.create(GeieImages.class);
   private MenuStateChangeHandler stateChangeHandler;

   private FontAndSizeMenu fontAndSizeMenu;
   private TextDecorationMenu decorationMenu;

   public TextStyleMenu(MenuStateChangeHandler stateChangeHandler, CellFormat cellFormat) {
      this.stateChangeHandler = stateChangeHandler;

      VerticalPanel fontPanel = new VerticalPanel();
      initWidget(fontPanel);

      HorizontalPanel upperFontPanel = new HorizontalPanel();
      fontAndSizeMenu = new FontAndSizeMenu(cellFormat);
      fontAndSizeMenu.addStyleName("geie-menu");
      fontAndSizeMenu.addStyleName("geie-font-size-menu");
      upperFontPanel.add(fontAndSizeMenu);
      fontPanel.add(upperFontPanel);

      HorizontalPanel lowerFontPanel = new HorizontalPanel();
      decorationMenu = new TextDecorationMenu(cellFormat);
      decorationMenu.addStyleName("geie-menu");
      decorationMenu.addStyleName("geie-text-decoration-menu");
      lowerFontPanel.add(decorationMenu);
      fontPanel.add(lowerFontPanel);
   }

   public void transferValues(CellFormat cellFormat) {
      fontAndSizeMenu.transferValues(cellFormat);
      decorationMenu.transferValues(cellFormat);
   }

   private class FontAndSizeMenu extends Composite implements StyleMenu {

      private ListBox fontListBox;
      private TextBox fontSizeBox;

      private FontAndSizeMenu(CellFormat cellFormat) {
         fontListBox = getFontListBox(cellFormat);
         fontSizeBox = getFontSizeBox(cellFormat);
         HorizontalPanel panel = new HorizontalPanel();
         panel.add(fontListBox);
         panel.add(fontSizeBox);
         initWidget(panel);
      }

      private TextBox getFontSizeBox(CellFormat cellFormat) {
         TextBox box = new TextBox();
         box.setText(cellFormat.getFontSize() + "");
         box.addBlurHandler(
               new BlurHandler() {
                  public void onBlur(BlurEvent event) {
                     stateChangeHandler.onStyleChanged();
                  }
               });
         box.addKeyDownHandler(
               new KeyDownHandler() {
                  public void onKeyDown(KeyDownEvent event) {
                     int code = event.getNativeKeyCode();
                     if (KeyCodes.KEY_ENTER == code) {
                        stateChangeHandler.onStyleChanged();
                     }
                  }
               });
         box.setMaxLength(3);
         box.addStyleName("geie-font-size-box");
         return box;
      }

      private ListBox getFontListBox(CellFormat cellFormat) {
         ListBox fontListBox = new ListBox();
         CellFormat.Font[] fonts = CellFormat.Font.values();
         for (int i = 0; i < fonts.length; i++) {
            CellFormat.Font font = fonts[i];
            fontListBox.addItem(font.toString());
            if (font.equals(cellFormat.getFont())) {
               fontListBox.setSelectedIndex(i);
            }
         }
         fontListBox.addChangeHandler(
               new ChangeHandler() {
                  public void onChange(ChangeEvent event) {
                     stateChangeHandler.onStyleChanged();
                  }
               });
         fontListBox.addStyleName("geie-font-family-list");
         return fontListBox;
      }

      public void transferValues(CellFormat cellFormat) {
         CellFormat.Font[] fonts = CellFormat.Font.values();
         CellFormat.Font font = fonts[fontListBox.getSelectedIndex()];
         cellFormat.setFont(font);
         int fontSize = Integer.parseInt(fontSizeBox.getText());
         cellFormat.setFontSize(fontSize);
      }
   }

   private class TextDecorationMenu extends Composite implements StyleMenu {

      private ToggleButton boldButton;
      private ToggleButton italicButton;
      private ToggleButton underlineButton;

      private TextDecorationMenu(CellFormat cellFormat) {
         HorizontalPanel panel = new HorizontalPanel();
         initWidget(panel);
         ClickHandler commonClickHandler = new ClickHandler() {
            public void onClick(ClickEvent event) {
               stateChangeHandler.onStyleChanged();
            }
         };
         boldButton = new ToggleButton(new Image(imageResources.textBold()));
         boldButton.addClickHandler(commonClickHandler);
         boldButton.setDown(cellFormat.isBold());
         boldButton.addStyleName("geie-toggle-button");
         boldButton.addStyleName("geie-bold-button");
         panel.add(boldButton);
         italicButton = new ToggleButton(new Image(imageResources.textItalic()));
         italicButton.addClickHandler(commonClickHandler);
         italicButton.setDown(cellFormat.isItalic());
         italicButton.addStyleName("geie-toggle-button");
         italicButton.addStyleName("geie-italic-button");
         panel.add(italicButton);
         underlineButton = new ToggleButton(new Image(imageResources.textUnderline()));
         underlineButton.addClickHandler(commonClickHandler);
         underlineButton.setDown(cellFormat.isUnderlined());
         underlineButton.addStyleName("geie-toggle-button");
         underlineButton.addStyleName("geie-underline-button");
         panel.add(underlineButton);
      }

      public void transferValues(CellFormat cellFormat) {
         cellFormat.setBold(boldButton.isDown());
         cellFormat.setItalic(italicButton.isDown());
         cellFormat.setUnderlined(underlineButton.isDown());
      }
   }
}
